import numpy as np
import matplotlib.pyplot as plt

# 设置随机种子（可选）
# np.random.seed(42)

# 参数
n_steps = 100

# 生成步长：+1 或 -1
steps = np.random.choice([1, -1], size=n_steps)
# steps = np.random.choice([1, -1], size=n_steps,p=[0.56,0.44])

# 计算位置序列
position = np.cumsum(steps)
position = np.insert(position, 0, 0)  # 添加初始位置 S_0 = 0

# 画图
plt.figure(figsize=(10, 6))
plt.plot(position, label='Random Walk Path')
plt.axhline(0, color='k', linestyle='--', alpha=0.5)
plt.title('1D Simple Symmetric Random Walk (n=100)')
plt.xlabel('Time Step')
plt.ylabel('Position')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()

